{extend name="_container/add"}

{block name="content"}
{if $r.id}
<input type="hidden" name="id" value="{$r.id}">
{/if}
<input type="hidden" name="table_id" value="{:input('table_id')}">

<table class="layui-table" style="margin: 0 20px">
    <tr>
        <th>中文名</th>
        <th>英文名</th>
        <th>类型</th>
        <th>长度</th>
        <th>允许为空</th>
        <th>默认值</th>
    </tr>
    {for start="1" end="40"}
    <tr>
        <td>
            <input type="text" placeholder="请输入中文名" name="comment[]" class="layui-input modelform_comment"
                    style="" autocomplete="off" onblur="translateWord(this)" tabindex="{$i}" {if $i==1}id="start_focus"{/if}>
        </td>
        <td>
            <input type="text" placeholder="请输入英文名" name="name[]"  class="layui-input modelform_name"
                    style="" autocomplete="off" mark="name" >
        </td>
        <td>
            <select name="type[]" class="select2" lay-ignore  style="min-width: 110px;" >
                <option value="varchar">varchar</option>
                <option value="int">int</option>
                <option value="tinyint">tinyint</option>
                <option value="double">double</option>
                <option value="char">char</option>
                <option value="text">text</option>
                <option value="longtext">longtext</option>
                <option value="json">json</option>
            </select>
        </td>
        <td>
            <input type="text" placeholder="长度" name="length[]"  class="layui-input modelform_name"
                   value="400" style="" autocomplete="off">
        </td>
        <td>
            <select name="is_null[]" class="select2" lay-ignore  style="min-width: 110px;" >
                <option value="1">是</option>
                <option value="0">否</option>
            </select>
        </td>
        <td>
            <input type="text" placeholder="默认值" name="default"  class="layui-input modelform_name"
                   value="{$r?$r['default']:''}" style="" autocomplete="off">
        </td>









    </tr>
    {/for}
</table>

    <div class="layui-input-inline">

    </div>





{/block}
{block name="js"}
<script>
    function save_form3(obj) {
        save_form2(obj);
        return false;
    }
</script>

<script>
    function translateWord(obj) {
        let word = obj.value;
        if (word) {
            $.get('/auto/util/getEnglishWithType?word=' + word, (data) => {
                let res = data.result.res;
                let type = data.result.type;
                $(obj).parents('tr').find("input[name='name[]']").val(res)
                $(obj).parents('tr').find("select[name='type[]']").val(type).select2();
                switch (type) {
                    case 'varchar':
                        $(obj).parents('tr').find("input[name='length[]']").val(400);
                        $(obj).parents('tr').find("input[name='default[]']").val('');
                        break;
                    case 'int':
                        $(obj).parents('tr').find("input[name='length[]']").val(11);
                        $(obj).parents('tr').find("input[name='default[]']").val('0');
                        break;
                    case 'tinyint':
                        $(obj).parents('tr').find("input[name='length[]']").val(1);
                        $(obj).parents('tr').find("input[name='default[]']").val(0);
                        break;
                    case 'double':
                        $(obj).parents('tr').find("input[name='length[]']").val('10,2');
                        $(obj).parents('tr').find("input[name='default[]']").val('0.00');
                        break;
                    case 'text':
                        $(obj).parents('tr').find("input[name='length[]']").val('');
                        $(obj).parents('tr').find("input[name='default[]']").val('');
                        break;
                    case 'longtext':
                        $(obj).parents('tr').find("input[name='length[]']").val('');
                        $(obj).parents('tr').find("input[name='default[]']").val('');
                        break;
                    case 'char':
                        $(obj).parents('tr').find("input[name='length[]']").val('32');
                        $(obj).parents('tr').find("input[name='default[]']").val('');
                        break;
                    case 'json':
                        $(obj).parents('tr').find("input[name='length[]']").val('');
                        $(obj).parents('tr').find("input[name='default[]']").val('');
                        break;
                }
            })
        }
    }
</script>


<script>
    $("select[name='type[]']").change(
        (obj) => {
            let type = obj.target.value;
            switch (type) {
                case 'varchar':
                    $(obj.target).parents('tr').find("input[name='length[]']").val(400);
                    $(obj.target).parents('tr').find("input[name='default[]']").val('');
                    break;
                case 'int':
                    $(obj.target).parents('tr').find("input[name='length[]']").val(11);
                    $(obj.target).parents('tr').find("input[name='default[]']").val('0');
                    break;
                case 'tinyint':
                    $(obj.target).parents('tr').find("input[name='length[]']").val(1);
                    $(obj.target).parents('tr').find("input[name='default[]']").val(0);
                    break;
                case 'double':
                    $(obj.target).parents('tr').find("input[name='length[]']").val('10,2');
                    $(obj.target).parents('tr').find("input[name='default[]']").val('0.00');
                    break;
                case 'text':
                    $(obj.target).parents('tr').find("input[name='length[]']").val('');
                    $(obj.target).parents('tr').find("input[name='default[]']").val('');
                    break;
                case 'longtext':
                    $(obj.target).parents('tr').find("input[name='length[]']").val('');
                    $(obj.target).parents('tr').find("input[name='default[]']").val('');
                    break;
                case 'char':
                    $(obj.target).parents('tr').find("input[name='length[]']").val('32');
                    $(obj.target).parents('tr').find("input[name='default[]']").val('');
                    break;
                case 'json':
                    $(obj.target).parents('tr').find("input[name='length[]']").val('');
                    $(obj.target).parents('tr').find("input[name='default[]']").val('');
                    break;
            }
        }
    )
</script>

<script>
    $('#start_focus').focus();
</script>
{/block}


{/extend}
